/*
 * dsm_launcher_broker.cpp
 *
 *  Created on: Dec 15, 2013
 *      Author: m
 */

#include "DSMComun.h"
#include "SharedMemory2.h"
#include "MessageQueue.h"
#include "Semaphore.h"
#include "Sequence.h"
#include "ColaRemotaTransmisor.h"
#include "ColaRemotaReceptor.h"
#include "CPrinter.h"
#include "StringUtils.h"
#include "ProcessAux.h"
#include <unistd.h>

using ipc::CPrinter;
using ipc::MessageQueue;
using ipc::SharedMemory2;
using ipc::Semaphore;
using ipc::Sequence;

int main ( int argc, char* argv[] )
{
    const int maximaCantidadDSM = 100;
    int *contadorPedidos;

    CPrinter::setHeader( "dsm_launcher_broker" );

    MessageQueue<PedidoDSM>::create( DSM_IPC_BASEFILE, COLA_PEDIDO_TOKEN );
    MessageQueue<PedidoDSM>::create( DSM_IPC_BASEFILE, COLA_ENTREGA_TOKEN );
    MessageQueue<TokenDSM>::create( DSM_IPC_BASEFILE, COLA_LIBERAR_TOKEN );

    Semaphore::create( DSM_IPC_BASEFILE, MTX_CONT_PEDIDOS, maximaCantidadDSM ).initialize( 1 );

    contadorPedidos = SharedMemory2<int>::create( DSM_IPC_BASEFILE, SHM_CONT_PEDIDOS, maximaCantidadDSM );
    for ( int i = 0; i < maximaCantidadDSM; i++ )
    {
        contadorPedidos[i] = 0;
    }

    CPrinter::printOut( "Inicializando Servicio de DSM" );

    int nTokens = 9;
    for ( int i = 2; i <= nTokens; i++ )
    {
        Argv args;
        args.pushArg( i );
        ProcessAux::launchProcess( "./dsmGestorEntrega", 1, args );
        ProcessAux::launchProcess( "./dsmGestorPedidos", 1, args );
        ProcessAux::launchProcess( "./gestorToken" + StringUtils::NumberToString<int>(i) );
    }
}
